import 'package:app/pages/car_modules/my_car_module/my_car_view.dart';
import 'package:app/pages/daily_mission_report_module/my_mission_module/my_mission_view.dart';
import 'package:app/pages/home_module/home_controller.dart';
import 'package:app/pages/reward_modules/reward_view.dart';
import 'package:app/router/app_pages.dart';
import 'package:app/services/address.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import '../../common/app_theme.dart';
import '../../common/colors.dart';
import '../../common/tool.dart';
import '../daily_calendar_report_module/my_calendar_module/my_calendar_page.dart';
import '../daily_work_report_module/mydaily_bottom_tabbar_page.dart';
import '../document_module/document_bottom_tabbar.dart';
import '../news_module/news_bottom_tabbar_page.dart';
import '../office_supplies_modules/office_supplies_view.dart';
import '../sign_in_module/sign_bottom_controller_view.dart';
import 'smart_drawer_page.dart';
import '../../utils/hexcolor.dart';

class HomeView extends GetView {

  HomeView({super.key});

  @override
  final HomeController controller = Get.put(HomeController());

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      drawer: const SmartDrawer(),
      // drawerDragStartBehavior:DragStartBehavior.down ,
      // resizeToAvoidBottomInset: true,
      appBar:PreferredSize(preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
      child: AppBar(
        // leading: Builder(
        //   ///自定义抽屉效果按钮
        //   builder: (BuildContext context) {
        //     return IconButton(
        //         icon: Icon(Icons.menu),
        //         onPressed: () {
        //           SearchModule.closeKeyboard(context);
        //           Scaffold.of(context).openDrawer();
        //         });
        //   },
        // ),
        centerTitle: false,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            GestureDetector(
              child: Row(
                children: [
                  Text(
                    '',
                    style:
                    TextStyle(color: HexColor('#232323'), fontSize: 20),
                  ),
                ],
              ),
              onTap: () {
                // Get.dialog(TopDiaLogPage(),barrierColor: Colors.transparent);
              },
            ),
            GestureDetector(
              child:  Image.asset(
                'images/home_add_icon.png',
                width: 24,
                height: 24,
              ),
              onTap: ()async {
                // Get.to(QrCodeView());
               var data = await Get.toNamed(AppRoutes.allGrid);
               if(data == 'refresh'){
                 controller.requestData();
               }
              },
            )
          ],
        ),
      ),),
      backgroundColor: AppColor.bgColor,
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode()); // 触摸收起键盘
        },
        child: GetBuilder<HomeController>(builder: (_) {
          return EasyRefresh.custom(
            // header: MaterialHeader(),
            onRefresh: () => controller.homePull(),
            slivers: [
              SliverToBoxAdapter(
                  child: GestureDetector(
                onTap: () async {
                  // Get.to(SearchPage(),transition: Transition.fade);
                },
                child: Container(
                    decoration:const BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(15))),
                    alignment: Alignment.centerLeft,
                    margin: const EdgeInsets.only(top: 15, left: 10, right: 10),
                    padding: const EdgeInsets.only(left: 15),
                    height: 35,
                    width: Get.width - 45,
                    child: Row(
                      children: [
                        // Icon(
                        //   Icons.search,
                        //   size: 20,
                        //   color: AppColor.smallTextColor,
                        // ),
                        Image.asset('images/top_search.png',width: 15,height: 15,),
                        Container(
                          alignment: Alignment.center,
                          // color: Colors.red,
                          width: Get.width - 100,
                          padding: const EdgeInsets.only(left: 5),
                          height: 45,
                          child: TextField(
                            onChanged: (values) {
                              controller.requestData(search: values);
                            },
                            decoration: InputDecoration(
                                isCollapsed: true,
                                border: InputBorder.none,
                                hintText: '请输入关键词',
                                hintStyle: TextStyle(
                                    color: AppColor.hintColor, fontSize: 13)),
                            style: TextStyle(
                                color: AppColor.smallTextColor, fontSize: 14),
                          ),
                        )
                      ],
                    )),
              )),
              SliverToBoxAdapter(
                child: Container(

                  margin: const EdgeInsets.only(top: 15, left: 15, right: 15),
                  clipBehavior: Clip.antiAlias,
                  decoration: const BoxDecoration(
                      color: Colors.red,
                    borderRadius: BorderRadius.all(Radius.circular(8))
                  ),
                  child: Image.asset('images/login_face_bg.png'),
                ),
              ),

              SliverList(
                delegate: _mySliverChildBuilderDelegate(),
              ),

              // GetBuilder<HomeController>(builder: (_) {
              //   return   SliverList(delegate: _mySliverChildBuilderDelegate(),);
              // }),
            ],
          );
        }),
      ),
    );
  }

  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
      (BuildContext context, int index) {
        return Container(
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(8)),
          ),

          margin: const EdgeInsets.only(top: 10,left: 15,right: 15),
          padding: const EdgeInsets.only(bottom: 15),

          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                  // color: Colors.red,
                  width: Get.width - 15,
                  padding: const EdgeInsets.only(top: 10, left: 20, right: 10),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        '${controller.model?.data?[index].categoryName}',
                        style: const TextStyle(
                            fontWeight: FontWeight.w600, fontSize: 18),
                      ),
                      GestureDetector(
                        onTap: () async {
                          var data = await Get.toNamed(AppRoutes.allGrid);
                          // Get.to(AllGridView());
                          // var data = await Get.to(AllModulePage());
                          if (data == 'refresh') {
                            controller.requestData();
                          }
                        },
                        child: Row(
                          children: [
                            Text(
                              '全部',
                              style: TextStyle(
                                  color: AppColor.smallTextColor, fontSize: 12),
                            ),
                            Icon(
                              Icons.arrow_forward_ios_outlined,
                              size: 11,
                              color: AppColor.smallTextColor,
                            )
                          ],
                        ),
                      )
                    ],
                  )),
              const SizedBox(
                height: 15,
              ),
              GridView.builder(
                physics: const NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                gridDelegate: _mySliverGridDelegateWithFixedCrossAxisCount(),
                itemBuilder: (BuildContext context, int a) {
                  return GestureDetector(
                    onTap: () {
                      var appsname =
                          controller.model?.data?[index].apps?[a].name;
                      if(appsname == '合同'){
                        Get.toNamed(AppRoutes.contract);
                      }
                     else if (appsname == '项目') {
                        // Get.to(MyProjectView());

                        Get.toNamed(AppRoutes.projectList);
                      }else if(appsname == '签到'){
                        Get.to(SignBottomControllerView());
                      }else if(appsname == '任务'){
                        Get.to(MyMissionView());
                      }else if(appsname == '日程'){
                        Get.to(const MYCalendarPage());
                      }else if(appsname == '请假'){
                       Get.toNamed(AppRoutes.leavePage);
                      }else if(appsname == '文档'){
                       // Get.toNamed(AppRoutes.document);
                       Get.to(const DocumentBottomTabBar());
                      }else if(appsname == '工时单'){
                       Get.toNamed(AppRoutes.timeSheetList);
                      }else if(appsname == '考勤'){
                       Get.to(SignBottomControllerView());
                      }else if(appsname == '公告'){
                       Get.to(const NewsBottomTabBarPage());
                      }else if(appsname == '我的车辆'){
                       Get.to(MyCarView());
                      }else if(appsname == '奖惩'){
                       Get.to(RewardView());
                      }else if(appsname == '办公用品'){
                       Get.to(OfficeSuppliesView());
                      }
                      else{
                        Get.to(const MyDailyBottomTabBarPage());
                      }
                    },
                    child: Container(
                      height: 50,
                      margin: const EdgeInsets.only(top: 5, left: 10,right: 10),
                      // color: Colors.orange,
                      // color: Colors.primaries[index % 8],
                      child: Column(
                        children: [
                       ExtendedImage.network(
                           '${Address.homeHost}${controller.model?.data?[index].apps?[a].icon}'),
                          Container(
                            alignment: Alignment.center,
                            // color: Colors.yellow,
                            padding: const EdgeInsets.only(left: 3,top: 3),
                            child: Text(
                              '${controller.model?.data?[index].apps?[a].name}',
                              style: appThemeData.textTheme.bodyText1
                                  ?.copyWith(fontSize: 12),
                            ),
                          )
                        ],
                      ),
                    ),
                  );
                },
                itemCount: controller.model?.data?[index].apps?.length ?? 0,
              ),
            ],
          ),
        );
      },
      childCount: controller.model?.data?.length ?? 0,
    );
  }

  _mySliverGridDelegateWithFixedCrossAxisCount() {
    return const SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 4,
      mainAxisSpacing: 10,
      crossAxisSpacing: 5,
      childAspectRatio: 1 / 1.2,
    );
  }
}
